// ol
import VectorSource from 'ol/source/Vector';
import VectorLayer from 'ol/layer/Vector';
import Feature from 'ol/Feature';
import { Style, Icon, Fill, Text } from 'ol/style';
// js
import { computed } from 'vue';
import * as UtilMethods from './util.js';
import * as turf from '@turf/turf';
// api
import { useUserStore } from '/@/store/modules/system/user';
const blackShipList = computed(() => useUserStore().getBlackShipList || []);


// 后端字段老是变来变去，定义一个常量吧
const codeProp = {
  lon: "longitude",
  lat: "latitude"
}

// 判断是否进入融合区域，假装他融合上了
function isInArea(point) {
  const areaList = [
    [119.4529773794631, 29.186996959411445],
    [119.4581272207717, 29.184636542629832],
    [119.45430775513451, 29.17365801759037],
    [119.44851418366233, 29.165901150239094],
    [119.44083233704367, 29.16957355664006],
    [119.45284863343039, 29.187034425906575],
    [119.45284863343039, 29.187034425906575],
    [119.4529773794631, 29.186996959411445],
  ];
  var pt = turf.point(point);
  var poly = turf.polygon([areaList]);
  return turf.booleanPointInPolygon(pt, poly);
}

export default {
  // 初始化图层
  initShipLayer: function () {
    let layer = null
    let source = null
    source = new VectorSource({
      features: []
    });
    layer = new VectorLayer({
      source: source,
      zIndex: 500
    });
    return { layer, source }
  },
  removeShip: function (shipSource, row) {
    var newshipFeature = shipSource.getFeatureById(row.targetId);
    if (newshipFeature) {
      shipSource.removeFeature(newshipFeature)
    }
  },
  // 三角形基础逻辑
  drawBaseShip: function (row, shipSource) {
    if (!row) return
    if (!row.targetId) return
    if (!shipSource) return
    // 判断当前是否存在这艘船，不存在就绘制，存在就更新坐标及样式
    var newshipFeature = shipSource.getFeatureById(row.targetId);
    if (newshipFeature === null) {
      shipSource.addFeature(this.setBaseShipFeature(row));
    } else {
      row = Object.assign({}, newshipFeature.values_.detail, row)
      newshipFeature.setGeometry(UtilMethods.pointLonToXy(row[codeProp.lon], row[codeProp.lat]));
      newshipFeature.setStyle(this.setBaseShipStyle(row))
      newshipFeature.values_.detail = row
    }
  },
  // 三角形基础信息
  setBaseShipFeature: function (row) {
    let shipFeature = new Feature({
      geometry: UtilMethods.pointLonToXy(row[codeProp.lon], row[codeProp.lat]),
      featureType: 'ship',
      detail: row
    });
    shipFeature.setId(row.targetId);
    shipFeature.setStyle(this.setBaseShipStyle(row))
    return shipFeature
  },
  // 船舶样式
  setBaseShipStyle: function (row) {
    let src, rotation;
    // const heading = row.heading || row.course;
    const heading = row.course;
    if (heading === 511 || heading === undefined || heading === null) {
      src = '/images/sign/ship_cir.png'
      rotation = 0
    } else {
      src = '/images/sign/ship_stop.png'
      rotation = Math.PI / (180 / heading)
    }
    const result = isInArea([row[codeProp.lon], row[codeProp.lat]])
    return new Style({
      image: new Icon({
        src,
        scale: 1, // 根据需要调整比例
        rotation,
        // color: row.dataType == '3' ? "#3ADEAB" : "#FFFF66"
        color: blackShipList.value[row.mmsi] ? "#333" : result ? "#3ADEAB" : "#FFFF66"
      }),
      // text: new Text({
      //   textAlign: 'center',
      //   text: `${row.mmsi}_${row.navStatus || 0}`,
      //   backgroundFill: new Fill({ color: "#fffb" }),
      //   offsetX: 15,
      //   offsetY: -15,
      //   overflow: true,
      // })
    })
  }
}